package com.qtenv.datasync.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GeoJSONTest
 * @Author CBL
 * @Date 2022/4/27 9:57
 **/
public class GeoJSONTest {
    public static void main(String[] args) {
        StringBuilder source = new StringBuilder();
        source.append("[[119.263514,29.461785],[119.25789,29.463221],[119.252602,29.463814],[119.243935,29.45848],[119.253768,29.450092],[119.25517,29.447071],[119.257806,29.441906],[119.248848,29.443359],[119.247466,29.435824],[119.246884,29.432616],[119.256123,29.428998],[119.257018,29.426155],[119.260268,29.424032],[119.261728,29.426748],[119.262932,29.429266],[119.266071,29.434035],[119.266981,29.437806],[119.266485,29.440884],[119.264259,29.444585],[119.265081,29.447796],[119.264577,29.451748],[119.261099,29.454909],[119.26497,29.458873],[119.266118,29.460873],[119.263514,29.461785]]");

        String target = source.toString().replaceAll("\t","").replaceAll("\n", "").replaceAll(" ", "")
                .replaceAll("1193\\.", "113.").replaceAll("],\\[", "]@[")
                .substring(1);
        target = target.substring(0, target.length() - 1);
        List<String> targetList = Arrays.asList(target.split("@"));
        List<String> handledList = CollectionUtil.newArrayList();
        for (String t : targetList) {
            String[] location = t.replace("[","").replace("]","").split(",");
            try {
                String trans = JSONUtil.toJsonStr(CoordinatesTransformUtil.transformWGS84ToBD09(Double.valueOf(location[0]), Double.valueOf(location[1])));

                String handled = trans.replace("[", "{lng:").replace(",", ", lat:").replace("]", "}");
                handledList.add(handled);
            }catch (Exception e){
                System.out.println(JSONUtil.toJsonStr(location));
            }
        }
        String lnglat = "[" + handledList.stream().collect(Collectors.joining(",")) + "]";
        System.out.println(lnglat);
    }
}
